local FightEffectManager = class("FightEffectManager")
local FightEffect = require("gameLogic.fight.fightEffect.FightEffect")
local FightConst = require("const.FightConst")

function FightEffectManager:ctor(excuter)
    self._excuter = excuter
    self._effectTriggerMaps = {}
    self:_initListenner()
end

function FightEffectManager:getExcuter()
    return self._excuter
end

function FightEffectManager:addFightEffect(id, entity)
    local config = require("config.fight_effect").get(id)
    local effect = FightEffect.new(config, entity)
    if not self._effectTriggerMaps[config.trigger] then
        self._effectTriggerMaps[config.trigger] = {}
    end
    printDebug("添加了特性 " .. effect:getEffectName() .. " 到 " .. entity:getDebugName())
    table.insert(self._effectTriggerMaps[config.trigger], effect)
end

function FightEffectManager:_initListenner()
    self._signalRoundbegin = G_SignalManager:add(SignalConst.EVENT_ROUND_BEGIN, handler(self, self._onEventRoundBegin))
    self._signalSheildBroken =
        G_SignalManager:add(SignalConst.EVENT_ENTITY_SHEILD_BROKEN, handler(self, self._onEventSheildBroken))
    self._signalSkillRelease =
        G_SignalManager:add(SignalConst.EVENT_SKILL_RELEASE, handler(self, self._onEventSkillRelease))
    self._signalEntityHurt = G_SignalManager:add(SignalConst.EVENT_ENTITY_HURT, handler(self, self._onEventEntityHurt))
    self._signalEntityAttack =
        G_SignalManager:add(SignalConst.EVENT_ENTITY_ATTACK, handler(self, self._onEventEntityAttack))
    self._signalEntityHurtTime =
        G_SignalManager:add(SignalConst.EVENT_ENTITY_HURT_TIME, handler(self, self._onEventEntityHurtTime))
end

function FightEffectManager:getSameEffectEntity(type, effectId)
    local list = self._effectTriggerMaps[type]
    local result = {}
    for i = 1, #list do
        if list[i]:getEffectId() == effectId then
            if list[i]:getEntity() and not list[i]:getEntity():isDie() then
                table.insert(result, list[i]:getEntity())
            end
        end
    end
    return result
end

function FightEffectManager:_effectList(type, param, battleReport)
    local list = self._effectTriggerMaps[type] or {}
    local removeList = {}
    for i = 1, #list do
        local effect = list[i]
        local isPlay = effect:effectPlay(param, battleReport, self)
        if isPlay then
            printDebug("特性 " .. effect:getEffectName() .. " 执行了")
        end
        if isPlay and effect:isDelTriggerAfter() then
            table.insert(removeList, effect)
        end
    end

    local removeEffect = function(list, effect)
        for i = 1, #list do
            if list[i] == effect then
                table.remove(list, i)
                return
            end
        end
    end
    for i = 1, #removeList do
        removeEffect(list, removeList[i])
    end
end

function FightEffectManager:_onEventRoundBegin(id, round, battleReport)
    local param = {}
    param.round = round
    self:_effectList(FightConst.EFFECT_TRIGGER_ROUND, param, battleReport)
end

function FightEffectManager:_onEventSheildBroken(id, entityId, battleReport)
    local param = {}
    param.entityId = entityId
    self:_effectList(FightConst.EFFECT_TRIGGER_SHEILD, param, battleReport)
end

function FightEffectManager:_onEventSkillRelease(id, entityId, battleReport)
    printDebug("FightEffectManager:_onEventSkillRelease")
    local param = {}
    param.entityId = entityId
    self:_effectList(FightConst.EFFECT_TRIGGER_SKILL, param, battleReport)
end

function FightEffectManager:_onEventEntityHurt(id, entityId, battleReport)
    local param = {}
    param.entityId = entityId
    self:_effectList(FightConst.EFFECT_TRIGGER_HURT, param, battleReport)
end

function FightEffectManager:_onEventEntityAttack(id, attackId, beAttackId, battleReport)
    local param = {}
    param.entityId = attackId
    param.beAttackId = beAttackId
    self:_effectList(FightConst.EFFECT_TRIGGER_ATTACK, param, battleReport)
end

function FightEffectManager:_onEventEntityHurtTime(id, entity, hurt, battleReport)
    local param = {}
    param.entityId = entitId
    param.hurt = hurt
    self:_effectList(FightConst.EFFECT_TRIGGER_HURT_TIME, param, battleReport)
end

function FightEffectManager:destroy()
    self._signalRoundbegin:remove()
    self._signalRoundbegin = nil
    self._signalSheildBroken:remove()
    self._signalSheildBroken = nil
    self._signalSkillRelease:remove()
    self._signalSkillRelease = nil
    self._signalEntityHurt:remove()
    self._signalEntityHurt = nil
    self._signalEntityAttack:remove()
    self._signalEntityAttack = nil
end

return FightEffectManager
